package com.tuanzi.loan.web.bean.family;

import com.tuanzi.loan.business.entity.family.LoanCreditCardInfo;
import com.tuanzi.loan.business.service.family.LoanCreditCardInfoService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.exception.SystemException;
import com.tuanzi.loan.vo.request.credit.LoanCreditCardInfoRequest;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.credit.LoanCreditCardInfoLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

/**
 * User: liangZongXi
 * Create time: 9:55 2018/8/31
 * Description:
 */

@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanCreditCardInfoListBean extends JSFListingBean<LoanCreditCardInfoRequest, LoanCreditCardInfo> {
    private static final long serialVersionUID = -988915395569337019L;
    @Autowired
    private LoanCreditCardInfoService loanCreditCardInfoService;

    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    private void initLazyDataModel() {
        $ = new LoanCreditCardInfoRequest();
        lazyDataModel = new LoanCreditCardInfoLazyDataModel($);
    }

    public void onRest(){
        initLazyDataModel();
    }

    public void offOrOnline(Long id, Integer status){
        if(null == id){
            addErrorMessage("找不到id");
            return;
        }

        if(null == status){
            addErrorMessage("找不到需要更新的状态");
            return;
        }

        LoanCreditCardInfo one = loanCreditCardInfoService.findOne(id);
        if(null == one){
            addErrorMessage("资源已删除");
            return;
        }
        one.setStatus(status);
        loanCreditCardInfoService.saveOrUpdate(one);
    }

    public String toInfo(Long id){
        if(null == id ){
            addErrorMessage("找不到id");
            throw new SystemException("找不到id");
        }
       return redirect("/views/family/credithousekeeper/credit-card-info.xhtml?id="+id);
    }
}
